import classnames from 'classnames'

import Icon from '@/components/MyIcon'
import { Image } from 'antd-mobile'
import styles from './index.module.scss'
import { Article } from '@/types/data'
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'
import relativeTime from 'dayjs/plugin/relativeTime'
import { useHistory } from 'react-router-dom'
dayjs.extend(relativeTime)
dayjs.locale('zh-cn')
type Props = {
  /**
   * 0 表示无图
   * 1 表示单图
   * 3 表示三图
   */
  // type?: 0 | 1 | 3
  item: Article
}

const ArticleItem = ({ item }: Props) => {
  const history = useHistory()
  const { type } = item.cover
  const goToArtInfo = () => {
    console.log(item)
    history.push('/article/' + item.art_id)
  }
  return (
    <div onClick={goToArtInfo} className={styles.root}>
      <div
        className={classnames(
          'article-content',
          type === 3 && 't3',
          type === 0 && 'none-mt'
        )}
      >
        <h3>{item.title}</h3>
        {type !== 0 && (
          <div className="article-imgs">
            {item.cover.images?.map((v, index) => {
              return (
                <div key={index} className="article-img-wrapper">
                  <Image lazy src={v} alt="" />
                </div>
              )
            })}
          </div>
        )}
      </div>
      <div className={classnames('article-info', type === 0 && 'none-mt')}>
        <span>{item.aut_name}</span>
        <span>{item.comm_count} 评论</span>
        <span>{dayjs(item.pubdate).fromNow()}</span>
        <span className="close">
          <Icon type="iconbtn_essay_close" />
        </span>
      </div>
    </div>
  )
}

export default ArticleItem
